package com.staff.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.AJAXResultMessage;
import com.staff.dao.StaffContractRepository;
import com.staff.entity.Staff;
import com.staff.entity.StaffContract;
import com.staff.entity.dto.StaffContractQueryDTO;

@Service
@Transactional
public class StaffContractService implements IStaffContractService {
	@Autowired
	private StaffContractRepository staffContractRepository;
	@Autowired
	private IStaffService staffService;
	@Autowired
	private IStaffContractService staffContractService;
	
	@Override
	public AJAXResultMessage saveOrUpdate(StaffContract staffContract) {
		Staff staff = staffService.findOne(staffContract.getStaffNum());

		staff.setStaffState("在职");//修改员工状态
		//判断合同类型更改员工类型
		if(staffContract.getContractType().equals("实习合同"))
		{
			staff.setStaffType("实习");
			staffContract.setStaff(staff);
			staffContractRepository.save(staffContract);
			
			return new AJAXResultMessage(true,"操作成功");
		}
		else if(staffContract.getContractType().equals("兼职劳动合同"))
		{
			staff.setStaffType("兼职");
			staffContract.setStaff(staff);
			staffContractRepository.save(staffContract);
			
			return new AJAXResultMessage(true,"操作成功");
		}
		else if(staffContract.getContractType().equals("劳动合同"))
		{
			staff.setStaffType("全职");
			staffContract.setStaff(staff);
			staffContractRepository.save(staffContract);
			
			return new AJAXResultMessage(true,"操作成功");
		}
		else
		{
			return new AJAXResultMessage(false,"合同类型不符！");
		}

	}

	@Override
	public void delete(StaffContract staffContract) {
		staffContract=staffContractRepository.findOne(staffContract.getId());
		if(staffContract!=null) {
			staffContract.setStaff(null);
			staffContractRepository.save(staffContract);
		}
		
		staffContractRepository.delete(staffContract);
	}

	@Override
	public StaffContract findOne(Long id) {
		return staffContractRepository.findOne(id);
	}

	@Override
	public List<StaffContract> findAll() {
		return (List<StaffContract>) staffContractRepository.findAll();
	}

	@Override
	public void delete(List<Long> ids) {
		List<StaffContract> staffContractList = (List<StaffContract>)staffContractRepository.findAll(ids);
		for(StaffContract staffContract : staffContractList) {
			staffContract.setStaff(null);
			staffContractRepository.save(staffContract);
		}
			staffContractRepository.delete(staffContractList);
		
	}

	@Override
	public Page<StaffContractQueryDTO> findAll(Specification<StaffContract> spec, Pageable pageable) {
		//将员工合同page转化为员工档案DTOpage
		List<StaffContractQueryDTO> results = new ArrayList<StaffContractQueryDTO>();
		Page<StaffContract> page = staffContractRepository.findAll(spec, pageable);
		for(StaffContract sc:page.getContent()) {
			StaffContractQueryDTO scDTO = new StaffContractQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<StaffContractQueryDTO>(results, pageable, results.size());
	}

	@Override
	public AJAXResultMessage saveOrUpdate(StaffContractQueryDTO staffContractQueryDTO) {
		StaffContract sc = new StaffContract();
		if(staffService.findOne(staffContractQueryDTO.getStaffNum())!= null)//判断员工是否存在数据库
		{
			if(staffContractQueryDTO.getId()== null)
			{//新增合同
				Staff staff = staffService.findOne(staffContractQueryDTO.getStaffNum());
				
				sc.setStaffName(staff.getStaffName());
				sc.setStaffNum(staffContractQueryDTO.getStaffNum());
				sc.setContractContent(staffContractQueryDTO.getContractContent());
				sc.setContractEndDate(staffContractQueryDTO.getContractEndDate());
				sc.setContractNum(staffContractQueryDTO.getContractNum());
				sc.setContractStarDate(staffContractQueryDTO.getContractStarDate());
				sc.setContractType(staffContractQueryDTO.getContractType());
				sc.setPosition(staffContractQueryDTO.getPosition());
				sc.setStaff(staff);
				
				return staffContractService.saveOrUpdate(sc);
			}
			else 
			{//修改合同
				sc = staffContractService.findOne(staffContractQueryDTO.getId());
				
				sc.setContractContent(staffContractQueryDTO.getContractContent());
				sc.setContractEndDate(staffContractQueryDTO.getContractEndDate());
				sc.setContractNum(staffContractQueryDTO.getContractNum());
				sc.setContractStarDate(staffContractQueryDTO.getContractStarDate());
				sc.setContractType(staffContractQueryDTO.getContractType());
				sc.setPosition(staffContractQueryDTO.getPosition());
				
				return staffContractService.saveOrUpdate(sc);
			}
		}
		return new AJAXResultMessage(false,"该员工不存在");
	}

}
